package com.fx.eduservice.controller;


import com.fx.commonuntils.ErrorEnum;
import com.fx.commonuntils.ReturnMessage;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.eduservice.client.VodClient;
import com.fx.eduservice.entity.vo.EduChapter;
import com.fx.eduservice.entity.vo.EduVideo;
import com.fx.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-07
 */
@Api(description = "课程小节系统")
@RestController
@RequestMapping("/eduservice/eduvideo")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
@CrossOrigin
public class EduVideoController {

    final private EduVideoService eduVideoService;
    final private VodClient vodClient;



    @ApiOperation(value = "通过章id获取小节信息")
    @GetMapping("/getVideoById/{videoId}")
    public ReturnMessage<EduVideo> getVideoById(@ApiParam(name = "videoId", value = "课程id", required = true)
                                                    @PathVariable String videoId) {
        EduVideo videoById = eduVideoService.getById(videoId);

        return ReturnMessageUtils.success(videoById);


    }





    @ApiOperation(value = "新增、修改小节")
    @PostMapping("/addVideo")
    public ReturnMessage<Object> addVideo(@RequestBody @Valid EduVideo eduVideo, BindingResult br) {
        log.info("Request:{}", eduVideo);
        if (eduVideo == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }

        boolean b = eduVideoService.saveOrUpdate(eduVideo);
        if (b) {
            return ReturnMessageUtils.success("新增小节成功",  eduVideo.getId());
        } else {
            return ReturnMessageUtils.error(ErrorEnum.E_EDU_TEACHER.getErrorCode(), ErrorEnum.E_EDU_TEACHER.getErrorMsg(), b);
        }

    }


    @ApiOperation(value = "删除小节")
    @PostMapping("/deleteVideo/{videoId}")
    public ReturnMessage<Object> deleteVideo(@ApiParam(name = "videoId", value = "课程id", required = true)
                                                   @PathVariable String videoId) {

        EduVideo video = eduVideoService.getById(videoId);

        //远程调用删除
        vodClient.removeAlyVideo(video.getVideoSourceId());


        return ReturnMessageUtils.success();


    }


    @ApiOperation(value = "根据课程id删除小节")
    @PostMapping("/deleteVideo/{coursmId}")
    public ReturnMessage<Object> deleteVideoByCoursmId(@ApiParam(name = "videoId", value = "课程id", required = true)
                                             @PathVariable String coursmId) {


        return eduVideoService.deleteVideoByCoursmId(coursmId);


    }











}

